package com.eboss2.sbs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.model.ServiceGoodsListInfo;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.tool.utils.utils.StringUtils;

import java.util.List;

public class ServiceOrderListAdapter extends BaseAdapter {

	private Context context;
	private List<ServiceGoodsListInfo> datas;
	private LayoutInflater inflater;
	private ViewHolder holder = null;

	public ServiceOrderListAdapter(Context context, List<ServiceGoodsListInfo> datas) {
		this.context = context;
		this.datas = datas;
	}

	@Override
	public int getCount() {
		if(datas!=null&&datas.size()>0)return datas.size();
		else return 0;
	}

	@Override
	public ServiceGoodsListInfo getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		
		inflater = LayoutInflater.from(context);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.listitem_service_order, null);
			
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			holder.tv_spec = (TextView) convertView.findViewById(R.id.tv_spec);
			holder.tv_new_amt = (TextView) convertView.findViewById(R.id.tv_new_amt);
			holder.tv_old_amt = (TextView) convertView.findViewById(R.id.tv_old_amt);
			holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		ServiceGoodsListInfo data = datas.get(position);

		holder.tv_content.setText(data.getGood_name());
		holder.tv_new_amt.setText("￥"+ StringUtils.formatIntMoney(data.getPromotion_price()));
		holder.tv_num.setText("x"+data.getGood_num());



		DisplayImageOptions options = new DisplayImageOptions.Builder()//
				.showImageForEmptyUri(R.drawable.ic_public_nophoto)
				.showImageOnFail(R.drawable.ic_public_nophoto)
				.imageScaleType(ImageScaleType.EXACTLY)
				.cacheInMemory(true)//
				.cacheOnDisk(true)//
				.bitmapConfig(android.graphics.Bitmap.Config.RGB_565)//
				.build();
		ImageLoader.getInstance().displayImage(data.getGood_img(), holder.img, options);


		return convertView;
	}

	public final class ViewHolder {
		private ImageView img;
		private TextView tv_content;
		private TextView tv_spec;
		private TextView tv_new_amt;
		private TextView tv_old_amt;
		private TextView tv_num;
	}

}
